package com.dlc.beijingrfid.net.protocol;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import com.dlc.commonlibrary.utils.RxUtil;
import com.dlc.beijingrfid.net.bean.BackBean;
import com.dlc.beijingrfid.net.bean.BucaoCategoryBean;
import com.dlc.beijingrfid.net.bean.DepartmentsBean;
import com.dlc.beijingrfid.net.bean.HospitalBean;
import com.dlc.beijingrfid.net.bean.OrderBean;
import com.dlc.beijingrfid.net.bean.OrderDetailBean;
import com.dlc.beijingrfid.net.bean.OrderWashDetailBean;
import com.dlc.beijingrfid.net.bean.ProducerBean;
import com.dlc.beijingrfid.net.bean.ProductListBean;
import com.dlc.beijingrfid.net.bean.RFIDListBean;
import com.dlc.beijingrfid.net.bean.RFIDSpecListBean;
import com.dlc.beijingrfid.net.bean.SpecListBean;
import com.dlc.beijingrfid.net.bean.StockCheckBean;
import com.dlc.beijingrfid.net.bean.UserBean;
import com.dlc.beijingrfid.net.bean.WashBean;
import com.dlc.beijingrfid.net.bean.WashRecordBean;
import com.dlc.beijingrfid.net.protocol.base.HttpProtocol;
import com.lzy.okgo.model.HttpParams;
import rx.Observable;
import rx.functions.Func1;

/**
 * 库存相关的协议
 */
public class StockProto extends HttpProtocol {

    private StockProto() {
    }

    private static class InstanceHolder {
        private static final StockProto sProtocol = new StockProto();
    }

    public static StockProto get() {
        return InstanceHolder.sProtocol;
    }

    /**
     * 获取布草分类
     *
     * @param error
     * @return
     */
    public Observable<BucaoCategoryBean> categorys(String parent_id,@Nullable String error) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("parent_id", parent_id);
        return rxPost("categorys", httpParams, BucaoCategoryBean.class, false, error);
    }

    /**
     * 获取规格分类
     * @param error
     * @return
     */
    public Observable<SpecListBean> spec(String category,String specId,@Nullable String error) {
        HttpParams httpParams = new HttpParams();
/*        httpParams.put("category_id", category);
        httpParams.put("id", specId);*/
        return rxPost("spec", httpParams, SpecListBean.class, true, error);
    }
    /**
     * 订单列表
     *
     * @param apiName
     * @param callback
     */
    public void getOrder(String apiName, String page,String pagesize,String hotel_id,Callback<OrderBean> callback) {
        HttpParams params = new HttpParams();
        params.put("page", page);
        params.put("pagesize", pagesize);
        if (!TextUtils.isEmpty(hotel_id)) {
            params.put("hotel_id", hotel_id);
        }
        post(apiName, params, OrderBean.class, true, callback);
    }
    /**
     * 订单列表详情
     *
     * @param id
     * @param callback
     */
    public void getOrderDetail(String apiName, String id, Callback<OrderDetailBean> callback) {
        HttpParams params = new HttpParams();
        params.put("id", id);
        post(apiName, params, OrderDetailBean.class, true, callback);

    }
    public void getBackOrderDetail(String apiName, String orderno, Callback<OrderDetailBean> callback) {
        HttpParams params = new HttpParams();
        params.put("orderno", orderno);
        post(apiName, params, OrderDetailBean.class, true, callback);

    }
    /**
     * 获取分类下的产品列表
     *
     * @param error
     * @return
     */
    public Observable<ProductListBean> goods(int category, @Nullable String error) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("category_id", category);
        return rxPost("goods", httpParams, ProductListBean.class, true, error);
    }
    /**
     * 获取分类下的产品列表
     *
     * @param error
     * @return
     */
    public Observable<ProductListBean> goods1(int category, @Nullable String error) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("spec_id", category);
        return rxPost("goods", httpParams, ProductListBean.class, true, error);
    }
    public Observable<ProducerBean> producer(@Nullable String error) {
        HttpParams httpParams = new HttpParams();
        return rxPost("producer", httpParams, ProducerBean.class, true, error);
    }
    public Observable<ProductListBean> goods2(@Nullable String error) {
        HttpParams httpParams = new HttpParams();
        return rxPost("goods", httpParams, ProductListBean.class, true, error);
    }
    /**
     * 医院列表
     * @param hotel_id
     * @param error
     * @return
     */
    public Observable<HospitalBean> gethotel(int hotel_id,@Nullable String error) {
        HttpParams httpParams = new HttpParams();
        if (hotel_id != 0) {
            httpParams.put("hotel_id", hotel_id);
        }
        return rxPost("hotel", httpParams, HospitalBean.class, true, error);
    }

    /**
     *科室列表
     * @param office_id
     * @param error
     * @return
     */
    public Observable<DepartmentsBean> getDepartments(int hotel_id,int office_id,@Nullable String error) {
        HttpParams httpParams = new HttpParams();
        if (hotel_id != 0) {
            httpParams.put("hotel_id", hotel_id);
        }
        if (office_id != 0) {
            httpParams.put("office_id", office_id);
        }
        return rxPost("office", httpParams, DepartmentsBean.class, true, error);
    }

    /**
     *人员列表
     * @param hotel_id
     * @param error
     * @return
     */
    public Observable<UserBean> getUser(int hotel_id,int office_id,@Nullable String error) {
        HttpParams httpParams = new HttpParams();
        if (hotel_id != 0) {
            httpParams.put("hotel_id", hotel_id);
        }
        if (office_id != 0) {
            httpParams.put("office_id", office_id);
        }
        return rxPost("user", httpParams, UserBean.class, true, error);
    }


    /**
     * 获取RFID列表
     *
     * @param category
     * @param error
     * @return
     */
    public Observable<RFIDListBean> rfid(int category, int spec, int page,String pagesize, @Nullable String error) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("category_id", category);
        httpParams.put("spec_id", spec);
        //httpParams.put("goods_id", category);
        //httpParams.put("id", rfid); //  	RFID存储ID
        //httpParams.put("warehouse_id", category);
        //httpParams.put("ware_frame_id", category);
        //httpParams.put("cardno", category);
        if (page != 0) {
            httpParams.put("page", page);
        }
        httpParams.put("pagesize", pagesize);

        return rxPost("rfid", httpParams, RFIDListBean.class, true, error);
    }

    /**
     * 获取RFID列表
     *
     * @param rfid
     * @return
     */
    public Observable<RFIDListBean.DataBean> query_by_rfid(String rfid,String page,String pagesize) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("rfids", rfid);
        httpParams.put("page", page);
        httpParams.put("pagesize", pagesize);
        return rxPost("rfid", httpParams, RFIDListBean.class, true, null).map(
                new Func1<RFIDListBean, RFIDListBean.DataBean>() {
                    @Override
                    public RFIDListBean.DataBean call(RFIDListBean rfidListBean) {
                        return rfidListBean.data;
                    }
                }).compose(RxUtil.<RFIDListBean.DataBean>rxSchedulerHelper());
    }

    /**
     * 获取RFID列表
     *
     * @param rfid
     * @return
     */
    public Observable<StockCheckBean.DataBean> stock_check(String rfid, String warehouse_id) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("rfids", rfid);
        httpParams.put("warehouse_id", warehouse_id);
        return rxPost("stock_check", httpParams, StockCheckBean.class, true, null).map(
                new Func1<StockCheckBean,StockCheckBean.DataBean>() {
                    @Override
                    public StockCheckBean.DataBean call(StockCheckBean rfidListBean) {
                        return rfidListBean.data;
                    }
                }).compose(RxUtil.<StockCheckBean.DataBean>rxSchedulerHelper());
    }

    /**
     * RFID分类
     *
     * @param rfid
     * @return
     */
    public Observable<RFIDSpecListBean> rfid_spec(String rfid) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("rfids", rfid);
        return  rxPost("rfid_spec", httpParams, RFIDSpecListBean.class, true, null);
    }

    /**
     * 新送洗订单
     *
     * @param
     * @return
     */
    public Observable<WashBean> order_wash_new(String rfids) {
        HttpParams httpParams = new HttpParams();
        //httpParams.put("id", id);

        httpParams.put("rfids", rfids);
       // httpParams.put("hotel_id", id);

        return rxPost("order_wash_new", httpParams, WashBean.class, true, null);
    }

    /**
     * 新增返洗订单
     *
     * @param
     * @return
     */
    public Observable<BackBean> order_back_new(String rfids) {
        HttpParams httpParams = new HttpParams();
        //httpParams.put("id", id);

        httpParams.put("rfids", rfids);

        return rxPost("order_back_new", httpParams, BackBean.class, true, null);
    }
    /**
     * 新增返洗订单
     *
     * @param
     * @return
     */
    public Observable<BackBean> order_deliver_new(String rfids,String order_wash_id,String order_back_id,String order_rent_id) {
        HttpParams httpParams = new HttpParams();
        //httpParams.put("id", id);

        httpParams.put("rfids", rfids);
        if (!TextUtils.isEmpty(order_wash_id)) {
            httpParams.put("order_wash_id", order_wash_id);
        }
        if (!TextUtils.isEmpty(order_back_id)) {
            httpParams.put("order_back_id", order_back_id);
        }
        if (!TextUtils.isEmpty(order_rent_id)) {
            httpParams.put("order_rent_id", order_rent_id);
        }

        return rxPost("order_deliver_new", httpParams, BackBean.class, true, null);
    }
    /**
     * 获取洗涤记录
     *
     * @param rfid
     * @param page
     * @param error
     * @return
     */
    public Observable<WashRecordBean> wash_record(String rfid, int page, @Nullable String error) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("cardno", rfid);
        if (page != 0) {
            httpParams.put("page", page);
        }
        return rxPost("wash_record", httpParams, WashRecordBean.class, true, error);
    }

    /**
     * 移库
     * @param warehouseId
     * @param wareFrameId
     * @param rfids
     * @return
     */
    public Observable<Object> warehouseMove(String warehouseId,String wareFrameId,String rfids) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("warehouse_id", warehouseId);
        httpParams.put("ware_frame_id", wareFrameId);
        httpParams.put("ids", rfids);
        return rxPost("warehouse_move", httpParams, Object.class, true, null);
    }

    /**
     * 送洗单详情,布草交接界面的数据
     *
     * @param
     * @return
     */
    public Observable<OrderWashDetailBean> OrderDetailOrderWashDetail(String orderno) {
        HttpParams httpParams = new HttpParams();
        httpParams.put("orderno", orderno);
        return rxPost("order_deliver_detail", httpParams, OrderWashDetailBean.class, true, null);
    }
}
